package com.school.manager.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.school.manager.common.Constants;
import com.school.manager.domaim.TecTask;
import com.school.manager.domaim.UserTask;
import com.school.manager.entity.ResponseEntity;
import com.school.manager.entity.po.TecTaskPo;
import com.school.manager.entity.po.UserTaskPo;
import com.school.manager.entity.vo.TecTaskVo;
import com.school.manager.entity.vo.UserTaskVo;
import com.school.manager.enums.ExceptionEnum;
import com.school.manager.enums.ResponseEnum;
import com.school.manager.enums.TaskStatusEnum;
import com.school.manager.exception.BussinessException;
import com.school.manager.service.ITecTaskService;
import com.school.manager.service.IUserTaskService;
import com.school.manager.utils.ConvertUtil;
import com.school.manager.utils.MyContextHolder;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * description：教师教学任务控制器
 * date：2021/10/133:18 下午
 */

@RestController
@Api(tags = "教师教学任务")
@RequestMapping(Constants.PROGRAM_PATH + "task")
public class TecTaskController {
	
	@Autowired
    private ITecTaskService tecTaskService;
	
	@Autowired
	private IUserTaskService userTaskService;

	@PostMapping("add")
    @ApiOperation("新增教师教学任务")
    @ApiImplicitParam(name = "教师教学任务", value = "tecTaskPo", dataTypeClass = TecTaskPo.class)
    public ResponseEntity add(@RequestBody TecTaskPo tecTaskPo) {
        return ResponseEnum.SUCCESS.success(tecTaskService.save(ConvertUtil.convertObj(tecTaskPo, TecTask.class)));
    }


    @PostMapping("edit")
    @ApiOperation("修改教师教学任务")
    @ApiImplicitParam(name = "教师教学任务", value = "tecTaskPo", dataTypeClass = TecTaskPo.class)
    public ResponseEntity edit(@RequestBody TecTaskPo tecTaskPo) {
        return ResponseEnum.SUCCESS.success(tecTaskService.updateById(ConvertUtil.convertObj(tecTaskPo, TecTask.class)));
    }
    
    @GetMapping("del/{id}")
    @ApiOperation("修改教师教学任务")
    @ApiImplicitParam(name = "教师教学任务主键", value = "id", dataTypeClass = String.class)
    public ResponseEntity del(@PathVariable("id") String id) {
    	// 删除任务
    	tecTaskService.removeById(id);
    	// 删除关联任务
    	userTaskService.remove(new LambdaQueryWrapper<UserTask>()
    			.eq(UserTask::getTaskId, id));
        return ResponseEnum.SUCCESS.success();
    }

    @GetMapping("get/{id}")
    @ApiOperation("教师教学任务详情")
    @ApiImplicitParam(name = "教师教学任务主键", value = "id", dataTypeClass = String.class)
    public ResponseEntity get(@PathVariable("id") String id) {
        TecTask tecTask = tecTaskService.getById(id);
        return ResponseEnum.SUCCESS.success(ConvertUtil.convertObj(tecTask, TecTaskVo.class));
    }
    
    @GetMapping("publish/{id}")
    @ApiOperation("发布教师教学任务详情")
    @ApiImplicitParam(name = "教师教学任务主键", value = "id", dataTypeClass = String.class)
    public ResponseEntity publish(@PathVariable("id") String id) {
        TecTask tecTask = tecTaskService.getById(id);
        if (!TaskStatusEnum.READY.getCode().equals(tecTask.getTaskStatus())) {
        		throw new BussinessException(ExceptionEnum.NO_READY_TASK);
		}
        tecTask.setTaskStatus(TaskStatusEnum.PUBLISHED.getCode());
        return ResponseEnum.SUCCESS.success(tecTaskService.updateById(tecTask));
    }
    
    @GetMapping("pull/{id}")
    @ApiOperation("领取教师教学任务详情")
    @ApiImplicitParam(name = "教师教学任务主键", value = "id", dataTypeClass = String.class)
    public ResponseEntity pull(@PathVariable("id") String id) {
    	TecTask tecTask = tecTaskService.getById(id);
    	if (!TaskStatusEnum.PUBLISHED.getCode().equals(tecTask.getTaskStatus())) {
    		throw new BussinessException(ExceptionEnum.NO_STATUS_PULL);
		}
    	String userId = MyContextHolder.getCurrentUser().getId();
    	int count = userTaskService.count(new LambdaQueryWrapper<UserTask>()
    			.eq(UserTask::getTaskId, id)
    			.eq(UserTask::getUserId, userId));
    	if (count > 0) {
			throw new BussinessException(ExceptionEnum.TASK_ALREADY_HAS);
		}
        tecTask.setTaskQuantity(tecTask.getTaskQuantity() + 1);
        tecTaskService.updateById(tecTask);
        UserTask userTask = new UserTask();
        userTask.setTaskId(id);
        userTask.setUserId(userId);
        userTaskService.save(userTask);
        return ResponseEnum.SUCCESS.success();
    }
    
    @GetMapping("finish/{id}")
    @ApiOperation("完成教师教学任务详情")
    @ApiImplicitParam(name = "教师教学任务主键", value = "id", dataTypeClass = String.class)
    public ResponseEntity finish(@PathVariable("id") String id) {
        TecTask tecTask = tecTaskService.getById(id);
        tecTask.setFinishQuantity(tecTask.getFinishQuantity() + 1);
        tecTaskService.updateById(tecTask);
        UserTask userTask = userTaskService.getOne(new LambdaQueryWrapper<UserTask>()
        		.eq(UserTask::getTaskId, id)
        		.eq(UserTask::getUserId, MyContextHolder.getCurrentUser().getId()));
        userTask.setFinishTime(System.currentTimeMillis());
        userTaskService.updateById(userTask);
        return ResponseEnum.SUCCESS.success();
    }

    @PostMapping("page")
    @ApiOperation("教师教学任务分页列表")
    @ApiImplicitParam(name = "教师教学任务Po", value = "tecTaskPo", dataTypeClass = TecTaskPo.class)
    public ResponseEntity page(@RequestBody TecTaskPo tecTaskPo) {
        return ResponseEnum.SUCCESS.success(tecTaskService.page(tecTaskPo));
    }
    
    @PostMapping("user/page")
    @ApiOperation("教师教学任务分页列表")
    @ApiImplicitParam(name = "教师教学任务Po", value = "tecTaskPo", dataTypeClass = TecTaskPo.class)
    public ResponseEntity userPage(@RequestBody UserTaskPo userTaskPo) {
    	Page<UserTaskVo> result = tecTaskService.userPage(userTaskPo);
        return ResponseEnum.SUCCESS.success(result);
    }
    
    @PostMapping("mypage")
    @ApiOperation("我的教师教学任务分页列表")
    @ApiImplicitParam(name = "我的教师教学任务Po", value = "tecTaskPo", dataTypeClass = TecTaskPo.class)
    public ResponseEntity myPage(@RequestBody TecTaskPo tecTaskPo) {
    	tecTaskPo.setUserId(MyContextHolder.getCurrentUser().getId());
    	Page<TecTaskVo> result = tecTaskService.myPage(tecTaskPo);
        return ResponseEnum.SUCCESS.success(result);
    }
}
